package com.ljy.my_study.java.死锁;

import java.util.concurrent.locks.ReentrantLock;

public class TestMain {

    static ReentrantLock lock1=new ReentrantLock();
    static ReentrantLock lock2=new ReentrantLock();

    public static void main(String[] args) {
//        ReentrantLock lock=new ReentrantLock();
//        lock.lock();
//        new TestMain().test();
//        lock.unlock();


        Thread t1=new Thread(()->{
            try{
                lock1.lock();
                Thread.sleep(1000L);
                lock2.lock();
                test();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock2.unlock();
                lock1.unlock();
            }
        });

        Thread t2=new Thread(()->{
            try{
                lock2.lock();
                Thread.sleep(1000L);
                lock1.lock();
                test();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock1.unlock();
                lock2.unlock();
            }
        });

        t1.setName("thread1");
        t2.setName("thread2");

        t1.start();
        t2.start();

    }

    public static void test(){
//        while(true){
//            System.out.println("1");
//        }
        System.out.println(Thread.currentThread().getName()+" execute test()");
    }
}
